package com.lk.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lk.base.result.Result;
import com.lk.web.entity.SysRole;
import com.lk.web.entity.SysUser;
import com.lk.web.service.SysRoleService;
import com.lk.web.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: lk-security-parent
 * @description: 用户管理
 * @author: Aspirin
 * @create: 2020-06-14 22:58
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
  private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
  private static final String HTML_PREFIX = "system/user/";
  @Autowired private SysUserService sysUserService;
  @Autowired private SysRoleService sysRoleService;

  /** 跳转用户管理页面 */
  @PreAuthorize("hasAuthority('sys:user')")
  @GetMapping(value = {"/", ""})
  public String user() {
    return HTML_PREFIX + "user-list";
  }

  /**
   * 分页查询用户列表
   *
   * @param page 分页对象: size, current
   * @param sysUser 查询条件 : username, mobile
   * @return json
   */
  @PreAuthorize("hasAuthority('sys:user:list')")
  @PostMapping("/page")
  @ResponseBody
  public Result page(Page<SysUser> page, SysUser sysUser) {
    return Result.ok(sysUserService.selectPage(page, sysUser));
  }

  /**
   * 跳转新增或修改页面
   *
   * @param id 用户id
   */
  @PreAuthorize("hasAnyAuthority('sys:user:add', 'sys:user:edit')")
  @GetMapping(value = {"/form", "/form/{id}"})
  public String form(@PathVariable(required = false) Long id, Model model) {
    // 1, 查询用户信息，包含了用户所拥有的角色
    SysUser user = sysUserService.findById(id);
    model.addAttribute("user", user);
    // 2, 查询出所有角色信息 sys_role
    List<SysRole> roleList = sysRoleService.list();
    model.addAttribute("roleList", roleList);
    return HTML_PREFIX + "user-form";
  }

  @PreAuthorize("hasAnyAuthority('sys:user:add', 'sys:user:edit')")
  @RequestMapping(
      method = {RequestMethod.POST, RequestMethod.PUT},
      value = "")
  public String saveOrUpdate(SysUser sysUser) {
    // 1. 保存到用户表, 要将选择的角色保存到用户角色中间表
    sysUserService.saveOrUpdate(sysUser);
    return "redirect:/user";
  }

  @PreAuthorize("hasAuthority('sys:user:delete')")
  @DeleteMapping("/{id}")
  @ResponseBody
  public Result deleteById(@PathVariable Long id) {
    // 假删除，只做更新
    sysUserService.deleteById(id);
    return Result.ok();
  }

  /**
   * 批量删除
   *
   * @param ids 用户id
   * @return 过滤请求参数：filterTarget 指定哪个参数; filterObject是集合中的每个元素如果value表达式为true的数据则不会被过滤， 否则就过滤掉
   */
  @PreFilter(filterTarget = "ids", value = "filterObject > 0")
  @RequestMapping("/batch/{ids}")
  @ResponseBody
  public Result deleteByIds(@PathVariable List<Long> ids) {
    return Result.ok(ids);
  }

  /** @return 过滤返回值：filterObject是返回值集合中的每一个元素，当表达式为true则对应元素会返回 */
  //  @PostFilter("filterObject != authentication.principal.username")
  //  @RequestMapping("/list")
  //  @ResponseBody
  //  public List<String> page() {
  //    List<String> userList = Lists.newArrayList("spring", "security", "admin");
  //    return userList;
  //  }
}
